﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Electric;
using YArchitech.MEP.Electric.Common;

namespace HYElectricSetting
{
    public partial class ConduitSizeForm : System.Windows.Forms.Form, IElectricSettingItem
	{
		public ConduitSizeForm(Autodesk.Revit.DB.Document document)
		{
			this.InitializeComponent();
			this.doc = document;
			this.isEdited = false;
		}

		private void ConduitSizeForm_Load(object sender, EventArgs e)
		{
			this.InitButtonImage();
			this.PrepareSettingData();
			this.InitConduitTypeCombox();
		}

		public bool IsEdited
		{
			get
			{
				return this.isEdited;
			}
			set
			{
				this.isEdited = value;
				HYElectricSettingMainForm hyelectricSettingMainForm = base.Tag as HYElectricSettingMainForm;
				bool flag = this.isEdited && hyelectricSettingMainForm != null;
				if (flag)
				{
					hyelectricSettingMainForm.ValidApplyButton();
				}
			}
		}

		public void SaveElectricSettingData()
		{
			ElectricSettingData.SaveConduitSettingData(this.doc);
		}

		private void InitButtonImage()
		{
			this.buttonNewSize.Image = Resources.GetImage("Add20");
			this.buttonEditSize.Image = Resources.GetImage("ReName20");
			this.buttonDelSize.Image = Resources.GetImage("Delete20");
			ToolTip toolTip = new ToolTip();
			toolTip.InitialDelay = 500;
			toolTip.AutoPopDelay = 3000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.buttonNewSize, "添加");
			toolTip.SetToolTip(this.buttonEditSize, "编辑");
			toolTip.SetToolTip(this.buttonDelSize, "删除");
		}

		private void PrepareSettingData()
		{
			ElectricSettingData.InitConduitSettingProcessData(this.doc);
		}

		private void InitConduitTypeCombox()
		{
			this.cmbConduitType.Items.Clear();
			foreach (ConduitTypeData item in ElectricSettingData.ConduitSettingProcessData)
			{
				this.cmbConduitType.Items.Add(item);
			}
			bool flag = this.cmbConduitType.Items.Count > 0;
			if (flag)
			{
				this.cmbConduitType.SelectedIndex = 0;
			}
		}

		private void UpdateDataGridView()
		{
			ConduitTypeData conduitTypeData = (ConduitTypeData)this.cmbConduitType.SelectedItem;
			bool flag = conduitTypeData == null;
			if (!flag)
			{
				this.dataGridView.Rows.Clear();
				foreach (int num in conduitTypeData.SizeSet)
				{
					DataGridViewRow dataGridViewRow = new DataGridViewRow();
					int index = this.dataGridView.Rows.Add(dataGridViewRow);
					dataGridViewRow = this.dataGridView.Rows[index];
					dataGridViewRow.Cells[0].Value = num;
				}
			}
		}

		private void SelectDataGridViewRow(int key)
		{
			foreach (object obj in ((IEnumerable)this.dataGridView.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				int num = (int)dataGridViewRow.Cells[0].Value;
				bool flag = key == num;
				if (flag)
				{
					dataGridViewRow.Selected = true;
					return;
				}
			}
			bool flag2 = this.dataGridView.Rows.Count > 0;
			if (flag2)
			{
				this.dataGridView.Rows[0].Selected = true;
			}
		}

		private void cmbConduitType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateDataGridView();
		}

		private void buttonAdd_Click(object sender, EventArgs e)
		{
			bool flag = this.cmbConduitType.Items.Count <= 0;
			if (!flag)
			{
				List<string> list = new List<string>();
				foreach (object obj in this.cmbConduitType.Items)
				{
					ConduitTypeData conduitTypeData = (ConduitTypeData)obj;
					list.Add(conduitTypeData.Name);
				}
				bool flag2 = this.cmbConduitType.Items.Count <= 0;
				if (!flag2)
				{
					CreateItemForm createItemForm = new CreateItemForm("新建线管类型", list);
					bool flag3 = createItemForm.ShowDialog() == DialogResult.OK;
					if (flag3)
					{
						ConduitTypeData conduitTypeData2 = this.FindItem(createItemForm.BaseName);
						bool flag4 = conduitTypeData2 != null;
						if (flag4)
						{
							ConduitTypeData conduitTypeData3 = new ConduitTypeData(conduitTypeData2);
							conduitTypeData3.Name = createItemForm.NewName;
							ElectricSettingData.ConduitSettingProcessData.Add(conduitTypeData3);
							this.InitConduitTypeCombox();
							this.cmbConduitType.SelectedItem = conduitTypeData3;
							this.IsEdited = true;
						}
					}
				}
			}
		}

		private void buttonDel_Click(object sender, EventArgs e)
		{
			bool flag = this.cmbConduitType.Items.Count == 1;
			if (flag)
			{
				TaskDialog.Show("盈建科提示", "不能删除最后的线管类型。");
			}
			else
			{
				int selectedIndex = this.cmbConduitType.SelectedIndex;
				bool flag2 = selectedIndex < 0 || MessageBox.Show("确定要删除选中项吗？", "盈建科提示", MessageBoxButtons.OKCancel) != DialogResult.OK;
				if (!flag2)
				{
					ConduitTypeData item = (ConduitTypeData)this.cmbConduitType.SelectedItem;
					ElectricSettingData.ConduitSettingProcessData.Remove(item);
					this.InitConduitTypeCombox();
					bool flag3 = selectedIndex < this.cmbConduitType.Items.Count;
					if (flag3)
					{
						this.cmbConduitType.SelectedIndex = selectedIndex;
					}
					this.IsEdited = true;
				}
			}
		}

		private void buttonNewSize_Click(object sender, EventArgs e)
		{
			ConduitTypeData conduitTypeData = (ConduitTypeData)this.cmbConduitType.SelectedItem;
			bool flag = conduitTypeData == null;
			if (!flag)
			{
				List<string> list = new List<string>();
				foreach (int num in conduitTypeData.SizeSet)
				{
					list.Add(num.ToString());
				}
				CreateSingleItemForm createSingleItemForm = new CreateSingleItemForm("新建", string.Empty, list);
				bool flag2 = createSingleItemForm.ShowDialog() == DialogResult.OK;
				if (flag2)
				{
					int num2 = Convert.ToInt32(createSingleItemForm.NewValue);
					bool flag3 = conduitTypeData.SizeSet.Contains(num2);
					if (!flag3)
					{
						conduitTypeData.SizeSet.Add(num2);
						conduitTypeData.SizeSet.Sort();
						this.UpdateDataGridView();
						this.SelectDataGridViewRow(num2);
						this.IsEdited = true;
					}
				}
			}
		}

		private void buttonEditSize_Click(object sender, EventArgs e)
		{
			ConduitTypeData conduitTypeData = (ConduitTypeData)this.cmbConduitType.SelectedItem;
			bool flag = conduitTypeData == null;
			if (!flag)
			{
				bool flag2 = this.dataGridView.SelectedRows.Count <= 0;
				if (!flag2)
				{
					DataGridViewRow dataGridViewRow = this.dataGridView.SelectedRows[0];
					bool flag3 = dataGridViewRow == null;
					if (!flag3)
					{
						int num = (int)dataGridViewRow.Cells[0].Value;
						List<string> list = new List<string>();
						foreach (int num2 in conduitTypeData.SizeSet)
						{
							list.Add(num2.ToString());
						}
						CreateSingleItemForm createSingleItemForm = new CreateSingleItemForm("编辑", num.ToString(), list);
						bool flag4 = createSingleItemForm.ShowDialog() == DialogResult.OK;
						if (flag4)
						{
							int num3 = Convert.ToInt32(createSingleItemForm.NewValue);
							bool flag5 = MathUtils.IsEqual((double)num, (double)num3, 0.1) || conduitTypeData.SizeSet.Contains(num3);
							if (!flag5)
							{
								conduitTypeData.SizeSet.Remove(num);
								conduitTypeData.SizeSet.Add(num3);
								conduitTypeData.SizeSet.Sort();
								this.UpdateDataGridView();
								this.SelectDataGridViewRow(num3);
								this.IsEdited = true;
							}
						}
					}
				}
			}
		}

		private void buttonDelSize_Click(object sender, EventArgs e)
		{
			ConduitTypeData conduitTypeData = (ConduitTypeData)this.cmbConduitType.SelectedItem;
			bool flag = conduitTypeData == null;
			if (!flag)
			{
				bool flag2 = this.dataGridView.SelectedRows.Count <= 0;
				if (!flag2)
				{
					DataGridViewRow dataGridViewRow = this.dataGridView.SelectedRows[0];
					bool flag3 = dataGridViewRow == null;
					if (!flag3)
					{
						int index = dataGridViewRow.Index;
						int item = (int)dataGridViewRow.Cells[0].Value;
						conduitTypeData.SizeSet.Remove(item);
						this.UpdateDataGridView();
						bool flag4 = index < this.dataGridView.Rows.Count;
						if (flag4)
						{
							this.dataGridView.Rows[index].Selected = true;
						}
						this.IsEdited = true;
					}
				}
			}
		}

		private ConduitTypeData FindItem(string conduitTypeName)
		{
			return ElectricSettingData.ConduitSettingProcessData.Find((ConduitTypeData item) => conduitTypeName == item.Name);
		}

		private Document doc = null;

		private bool isEdited = false;
	}
}
